

from torchvision.models.vision_transformer import vit_b_16
from torchvision.io import decode_image,read_file
from torchvision import transforms
from PIL import Image
import torch
import torch.nn as nn


img_path = r"C:\Users\caofei\Pictures\1.jpg"



preprocess = transforms.Compose([
    transforms.Resize((224, 224)),  # 调整尺寸
    transforms.ToTensor(),  # 关键：转换为float32张量，像素值归一化到[0,1]
    transforms.Normalize(  # 标准化（可选，但模型预训练时通常会用）
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225]
    )
])


img = Image.open(img_path)

img_tensor = preprocess(img)




# r = read_file(img_path)
# img = decode_image(r)

img_tensor = img_tensor.unsqueeze_(0)
# process = transforms.Resize((224,224))
# img = process(img)

# print(img.float())
print(img_tensor)

model = vit_b_16(pretrained=True)
r = model(img_tensor)
print(r.size())
max_num = torch.argmax(r,dim=-1)
print(max_num)


for name,paramater in model.named_parameters():
    # print(name,paramater.shape)
    print(paramater.requires_grad)
    # print(name)






